<?php
    /**
     * User zgh
     * Email 1803897171@qq.com
     * Date 2021/7/13
     **/

    namespace app\common\service;


    use think\Model;

    class ResponseService
    {
        const DEFAULT_SUCCESS_CODE = 0; //默认成功返回码

        const DEFAULT_FAIL_CODE = -404; //默认的错误码

        const  DEFAULT_SUCCESS_MSG = 'ok';

        const DEFAULT_FAIL_MSG = "fail"; //默认的错误码

        public static function successJsonResponse($msg, $data = [], $ext = [], $code = "")
        {
            if (true == is_array($msg)) {
                $data = $msg;
                $msg = self::DEFAULT_SUCCESS_MSG;
            }
            if (!is_numeric($code)) {
                $code = self::DEFAULT_SUCCESS_CODE;
            }
            $arr = compact('code', 'msg', 'data');
            if (!empty($ext)) {
                $arr = array_merge($arr, $ext);
            }
            return json($arr, 200);
        }

        public static function JsonResponse($msg, $data = [], $ext = [], $code = "")
        {
            if (true == is_array($msg)) {
                $data = $msg;
                $msg = self::DEFAULT_FAIL_MSG;
            }
            if (!is_numeric($code)) {
                $code = self::DEFAULT_FAIL_CODE;
            }
            $arr = compact('code', 'msg', 'data');
            if (!empty($ext)) {
                $arr = array_merge($arr, $ext);
            }
            return json($arr, 200);
        }


    }